package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.view.image.CircleImageView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;
import com.sam.im.heiqun.utils.StringUtils;

/**
 * 发起AA收款
 */
public class SendAAActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_discribe)
    TextView tvDiscribe;
    @BindView(R.id.ed_nums)
    EditText edNums;
    @BindView(R.id.ed_money)
    EditText edMoney;
    @BindView(R.id.ed_content)
    EditText edContent;
    @BindView(R.id.sure_bt)
    Button sureBt;
    @BindView(R.id.tv_amtmoney)
    TextView tvAmtMoney ;
    @BindView(R.id.tv_amtnums)
    TextView tvAmtNums ;
    private String nums;
    private String money;
    private double f1;

    @BindView(R.id.relative_title)
    RelativeLayout relative_title ;

    @Override
    public int getContentViewId() {
        return R.layout.activity_send_aa;
    }
    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.send_aa_getmoney);
    }



    @Override
    protected void initViews(Bundle savedInstanceState) {

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(R.color.title_aa));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        relative_title.setBackgroundColor(getResources().getColor(R.color.title_aa));

        GlideUtils.loadHeadCircularImage(this, ToolsUtils.getUser().getHeadUrl(),imgIcon);
        nums = edNums.getText().toString().trim();
        money = edMoney.getText().toString().trim();
        sureBt.setEnabled(false);
        edNums.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

           }

           @Override
           public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

           }

           @Override
           public void afterTextChanged(Editable editable) {
               String s = editable.toString();
               String smoney = edMoney.getText().toString().trim();
               if (!StringUtils.isEmpty(s)&&!StringUtils.isEmpty(smoney)){
                   double v = Double.parseDouble(smoney);
                   double v1 = Double.parseDouble(s);
                   if (v1 < 2){
                       showToast(getResources().getString(R.string.aa_person_bottom2));
                       return;
                   }
                   double v2 = v / v1;

                   if (v <= 0){
                       showToast(getResources().getString(R.string.aa_getmoney_bottom0));
                       return;
                   }
                   if ( v > 5000){
                       showToast(getResources().getString(R.string.aa_getmoney_out5000));
                       edMoney.setText("5000");
                       return;
                   }
                   if (v2 <= 0.01){
                       showToast(getResources().getString(R.string.aa_getmoney_bottom1));
                       return;
                   }

                   BigDecimal b =new  BigDecimal(v2);
                   f1 = b.setScale(2, BigDecimal.ROUND_UP).doubleValue();
                   tvAmtNums.setText(getResources().getString(R.string.get_other)+(Integer.parseInt(s)-1)+getResources().getString(R.string.aa_pers_mei));
                   tvAmtMoney.setText(f1 +getResources().getString(R.string.yuan));
                   sureBt.setEnabled(true);
                   sureBt.setBackgroundResource(R.drawable.selector_sure_bt);
               }else {
                   tvAmtNums.setText("");
                   tvAmtMoney.setText("");
                   sureBt.setEnabled(false);
                   sureBt.setBackgroundResource(R.drawable.shape_sure_bt_false);
               }
           }
       });
        edMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                String snums = edNums.getText().toString().trim();
                if (!StringUtils.isEmpty(s)&&!StringUtils.isEmpty(snums)){
                    double v = Double.parseDouble(snums);
                    double v1 = Double.parseDouble(s);
                    double v2 = v1 / v;
                    if (v < 2){
                        showToast(getResources().getString(R.string.aa_person_bottom2));
                        return;
                    }
                    if (v1 <= 0){
                        showToast(getResources().getString(R.string.aa_getmoney_bottom0));
                        return;
                    }
                    if ( v1 > 5000){
                        showToast(getResources().getString(R.string.aa_getmoney_out5000));
                        edMoney.setText("5000");
                        return;
                    }
                    if (v2 <= 0.01){
                        showToast(getResources().getString(R.string.aa_getmoney_bottom1));
                        return;
                    }
                    BigDecimal b =new  BigDecimal(v2);
                    f1 = b.setScale(2,BigDecimal.ROUND_UP).doubleValue();
                    tvAmtNums.setText(getResources().getString(R.string.get_other)+(Integer.parseInt(snums)-1)+getResources().getString(R.string.aa_pers_mei));
                    tvAmtMoney.setText(f1+getResources().getString(R.string.yuan));
                    sureBt.setEnabled(true);
                    sureBt.setBackgroundResource(R.drawable.selector_sure_bt);
                }else {
                    tvAmtNums.setText("");
                    tvAmtMoney.setText("");
                    sureBt.setEnabled(false);
                    sureBt.setBackgroundResource(R.drawable.shape_sure_bt_false);
                }
            }
        });
    }
    @OnClick({R.id.pre_v_back, R.id.sure_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:
                finish();
                break;
            case R.id.sure_bt:
                String content = "" ;
                String trim = edContent.getText().toString().trim();
                if (StringUtils.isEmpty(trim)){
                    content = getResources().getString(R.string.aa_brife);
                }else {
                    content = trim ;
                }

                if (trim.length() > 10){
                    showToast("留言长度不能大于10");
                    return;
                }

                Intent intent = new Intent();
                intent.putExtra("nums",edNums.getText().toString().trim());
                intent.putExtra("money",edMoney.getText().toString().trim());
                intent.putExtra("discrible",content);
                intent.putExtra("amt",f1+"");
                setResult(-1,intent);
                finish();
                break;
        }
    }
}
